package com.yidu.t286.controller;
/*
 * @description:
 * @author:文杰
 * @date  :2021/7/29 0029下午 8:01
 */

import com.yidu.t286.biz.impl.CarRoleBizImpl;
import com.yidu.t286.biz.impl.FunBizImpl;
import com.yidu.t286.domain.Fun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class CarRoleController {

        @Autowired
        private CarRoleBizImpl carRoleBiz;

        @Autowired
        private FunBizImpl funBiz;

    /**
     * 显示权限树
     * @param roleId
     * @return
     */
    @GetMapping("/edit/{roleId}")
    public List<Fun> findTree(@PathVariable int roleId){
        //通过业务对象调用查找树方法返回数集合
        List<Fun> threeList=funBiz.selectFolders(roleId);
        return threeList;
    }

    /**
     * 显示权限
     *
     * @return 角色树集合
     */
    @RequestMapping("/rights")
    public List findAll(int roleId){
        //通过业务对象调用查找树方法返回数集合
        List<Fun> threeList=funBiz.selectFolder(roleId);

        //定义map集合传输logo参数
        Map logoMap=new HashMap();
        //添加键title，值为空
        logoMap.put("title","");
        //添加键image
        logoMap.put("image","/img/logo2.png");
        //添加键href，值为空
        logoMap.put("href","");

        //定义map集合传输首页参数
        Map homeMap=new HashMap();
        homeMap.put("title","首页");
        homeMap.put("href","candlestick-sh.html");
        //头部模块
        List<Map> list = new ArrayList<Map>();


        //空
        HashMap<Object, Object> map1 = new HashMap<>();
        //设置头部模块的标题、图标、路径为空
        map1.put("title","");
        map1.put("icon","");
        map1.put("href","");
        //父类模块
        List<Map> list1=new ArrayList<>();
        for (Fun fun : threeList) {
            //父级菜单集合
            Map<String ,Object> fathMap=new HashMap<>();
            //子模块.
            List<Map> list2=new ArrayList<>();
            //创建子菜单集合
            List<Fun> child = fun.getChild();
            //循环子菜单集合
            for (Fun fun1 : child) {
                //判断子菜单对象不为空
                if (fun1!=null && fun!=null){
                    if (fun1.getParentId().equals(fun.getId())){
                        //子菜单集合
                        Map<String ,Object> childMap=new HashMap<>();
                        //向子菜单集合中添加键为title
                        childMap.put("name",fun1.getTitle());
                        //向子菜单集合中添加键为icon
                        childMap.put("iconCls",fun1.getIcon());
                        //向子菜单集合中添加键为href
                        childMap.put("href",fun1.getHref());
                        //向子菜单集合中添加键为target,值为_self
                        childMap.put("target","_self");
                        childMap.put("path",fun1.getPaths());
                        childMap.put("component",fun1.getComponents());
                        //向子模块中添加子菜单集合
                        list2.add(childMap);
                    }
                }
            }
            //向父菜单集合中添加键为title
            fathMap.put("name", fun.getTitle());
            //向父菜单集合中添加键为icon
            fathMap.put("iconCls", fun.getIcon());
            fathMap.put("path",fun.getPaths());
            fathMap.put("component",fun.getComponents());
            //向父菜单集合中添加键为href,值为空
            fathMap.put("href", "");
            //向父菜单集合中添加键为target,值为_self
            fathMap.put("target", "_self");
            //向父菜单集合中添加键为child,值为子模块集合
            fathMap.put("children",list2);
            //向父模块中添加父菜单集合
            list1.add(fathMap);
        }
        //向空集合map1中添加键为child,值为父模块
        map1.put("child",list1);
        //向头部集合List中添加map1集合
        list.add(map1);
        //创建map集合
        Map map=new HashMap();
        //向map集合中添加键logoInfo,值为logo参数集合
        //map.put("logoInfo",logoMap);
        //向map集合中添加键homeInfo,值为首页参数集合
        //map.put("homeInfo",homeMap);
        //向map集合中添加键为menuInfo，值为头部集合list
        map.put("menuInfo",list);
        return list1;
    }
}
